package com.example.music.utils;

import com.example.music.enums.CodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@ApiModel(description = "响应结果")
@Data
public class ResponseMessage<T> implements Serializable {
    private static final long serialVersionUID = 8992436576262574064L;

    @ApiModelProperty("错误消息")
    protected String message;

    @ApiModelProperty("成功时响应内容")
    protected T result;

    @ApiModelProperty(value = "状态码", required = true)
    protected int status;

    @ApiModelProperty(value = "业务自定义状态码")
    protected String code;

    @ApiModelProperty(value = "时间戳", required = true, dataType = "Long")
    protected Long timestamp;

    private ResponseMessage<T> putTimeStamp(){
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public ResponseMessage<T> result(T result){
        this.result = result;
        return this;
    }

    public ResponseMessage<T> code(String code){
        this.code = code;
        return this;
    }

    public ResponseMessage<T> status(int status){
        this.status = status;
        return this;
    }

    public static <T> ResponseMessage<T> ok(T result){
        return new ResponseMessage<T>()
                .result(result)
                .putTimeStamp()
                .code(CodeEnum.SUCCESS.getValue())
                .status(200);
    }
    public static <T> ResponseMessage<T> ok(){
        return new ResponseMessage<T>()
                .putTimeStamp()
                .code(CodeEnum.SUCCESS.getValue())
                .status(200);
    }

    public static <T> ResponseMessage<T> error(String message) {
        return error(500, CodeEnum.UNKNOWN.getValue(), message);
    }

    public static <T> ResponseMessage<T> error(String message, Object... args) {
        return error(500, message, args);
    }

    public static <T> ResponseMessage<T> error(int status, String message) {
        return error(status, CodeEnum.UNKNOWN.getValue(), message);
    }

    public static <T> ResponseMessage<T> error(int status, String message, Object... args) {
        return error(status, CodeEnum.UNKNOWN.getValue(), message, args);
    }

    public static <T> ResponseMessage<T> error(int status, String code, String message) {
        return error(status, code, message, null);
    }

    public static <T> ResponseMessage<T> error(int status, String code, String message, Object... args) {
        ResponseMessage<T> msg = new ResponseMessage<>();
        msg.message = message;
        msg.status(status);
        msg.code(code);
        return msg.putTimeStamp();
    }
}
